import 'package:flutter/material.dart';
import 'package:gw_github_flutter/common/route_name.dart';
import 'package:gw_github_flutter/route/alertOrDialog/gwNetworkCustomDialog.dart';
import 'package:package_info_plus/package_info_plus.dart';

class AlertOrDialogRoute extends StatefulWidget {
  AlertOrDialogRoute({Key? key}) : super(key: key);

  @override
  State<AlertOrDialogRoute> createState() => _AlertOrDialogRouteState();
}

class _AlertOrDialogRouteState extends State<AlertOrDialogRoute> {
  late TextStyle? dialogTextStyle;
  PackageInfo? packageInfo;

  @override
  void initState() {
    super.initState();

    _initPackageInfo();
  }

  @override
  Widget build(BuildContext context) {
    final _scaffoldKey = GlobalKey<ScaffoldState>();
    final ThemeData theme = Theme.of(context);
    dialogTextStyle = theme.textTheme.subtitle1
        ?.copyWith(color: theme.textTheme.caption?.color);
    return Scaffold(
        appBar: AppBar(
          title: Text(RouteName.buttonRoute),
        ),
        body: _body());
  }

  Widget _body() {
    return SingleChildScrollView(
      child: Column(
        children: [
          const Divider(),
          _oneNetWorkCustomDialog(),
          const Divider(),
          _twoAssetsCustomDialog(),
          const Divider(),
          _threeSystemAlertDialog(),
          const Divider(),
          _fourSystemSimpleDialog(),
          const Divider(),
          _fiveSystemAboutDialog(),
          const Divider(),
        ],
      ),
    );
  }

  Widget _oneNetWorkCustomDialog() {
    return ElevatedButton(
      onPressed: () {
        showDialog(
            context: context,
            builder: (_) => GwNetworkCustomDialog(
                  image: Image.network(
                    "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Ffile03.16sucai.com%2F2017%2F1100%2F16sucai_P591F3A055.JPG&refer=http%3A%2F%2Ffile03.16sucai.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1665921127&t=22b1b8ea5899fedcbc077243219bbb32",
                    fit: BoxFit.cover,
                  ),
                  title: Text(
                    'Granny Eating Chocolate',
                    textAlign: TextAlign.center,
                    style: dialogTextStyle,
                  ),
                  description: const Text(
                    'This is a granny eating chocolate dialog box. This library helps you easily create fancy giffy dialog.',
                    textAlign: TextAlign.center,
                  ),
                  onOkButtonPressed: () {},
                ));
      },
      child: const Text('_oneNetWorkCustomDialog'),
    );
  }

  Widget _twoAssetsCustomDialog() {
    return ElevatedButton(
      onPressed: () {
        showDialog(
            context: context,
            builder: (_) => GwNetworkCustomDialog(
                  image: Image.asset(
                    'imgs/loading.gif',
                    fit: BoxFit.cover,
                  ),
                  title: Text(
                    'Granny Eating Chocolate',
                    textAlign: TextAlign.center,
                    style: dialogTextStyle,
                  ),
                  description: const Text(
                    'This is a granny eating chocolate dialog box. This library helps you easily create fancy giffy dialog.',
                    textAlign: TextAlign.center,
                  ),
                  onOkButtonPressed: () {},
                ));
      },
      child: const Text('_twoAssetsCustomDialog'),
    );
  }

  Widget _threeSystemAlertDialog() {
    return ElevatedButton(
      onPressed: () {
        showDialog(
          context: context,
          builder: (context) {
            return AlertDialog(
              shape: const RoundedRectangleBorder(
                  borderRadius: BorderRadius.all(Radius.circular(10))),
              title: const Text('提示'),
              //标题
              titlePadding: EdgeInsets.all(20),
              //标题的padding值
              content: Text(
                'test',
                style: dialogTextStyle,
              ),
              //弹框展示主要内容
              contentPadding: EdgeInsets.only(left: 20, right: 20),
              //内容的padding值
              actions: <Widget>[
                //操作按钮数组
                TextButton(
                  onPressed: () {
                    print("取消");
                    Navigator.pop(context);
                  },
                  child: const Text('取消'),
                ),
                TextButton(
                  onPressed: () {
                    print('确定');
                    Navigator.pop(context);
                  },
                  child: const Text('确定'),
                ),
              ],
            );
          },
          barrierDismissible: false,
        );
      },
      child: const Text("_threeSystemAlertDialog"),
    );
  }

  Widget _fourSystemSimpleDialog() {
    return ElevatedButton(
      onPressed: () {
        showDialog(
            context: context,
            builder: (context) {
              return SimpleDialog(
                title: Text('Info', style: dialogTextStyle), //标题
                titlePadding: EdgeInsets.all(20),
                contentPadding: EdgeInsets.all(0), //标题的padding值
                children: <Widget>[
                  ListTile(
                    leading: const CircleAvatar(
                      backgroundColor: Colors.blue,
                      child: Icon(Icons.account_circle),
                    ),
                    title: const Text('username@gmail.com'),
                    onTap: () {
                      Navigator.pop(context);
                    },
                  ),
                  ListTile(
                    leading: const CircleAvatar(
                      backgroundColor: Colors.red,
                      child: Icon(Icons.account_balance),
                    ),
                    title: const Text('user02@gmail.com'),
                    onTap: () {
                      Navigator.pop(context);
                    },
                  ),
                  const ListTile(
                    leading: CircleAvatar(
                      backgroundColor: Colors.grey,
                      child: Icon(Icons.add_circle),
                    ),
                    title: Text('add account'),
                    onTap: null,
                  ),
                ],
              );
            });
      },
      child: const Text("_fourSystemSimpleDialog"),
    );
  }

  Widget _fiveSystemAboutDialog() {
    return ElevatedButton(
      onPressed: () {
        showDialog(
            context: context,
            // 设置点击 dialog 外部不取消 dialog，默认能够取消
            barrierDismissible: false,
            builder: (context) {
              return AboutDialog(
                applicationName: packageInfo?.appName,
                applicationIcon: Image.asset('imgs/avatar-default.png',
                    width: 60.0, height: 60.0),
                applicationVersion: packageInfo?.version,
                //版本号，默认为空
                applicationLegalese: '版权所有：SCL',
                children: const <Widget>[
                  Text("具体的内容"),
                  Text('具体的布局'),
                ],
              );
            });
      },
      child: const Text("_fiveSystemAboutDialog"),
    );
  }

  Widget _sixSystemAboutListTile() {
    return AboutListTile(
        icon: CircleAvatar(
            child: Icon(Icons.update, color: Colors.white, size: 20.0),
            maxRadius: 15),
        child: Text('AboutListTile'),
        applicationName: packageInfo?.appName,
        applicationIcon:
            Image.asset('images/flutter_logo.png', width: 60.0, height: 60.0),
        applicationVersion: packageInfo?.version,
        //版本号，默认为空
        applicationLegalese: '版权所有：SCL',
        aboutBoxChildren: <Widget>[Text("BoxChildren"), Text("box child 2")]);
  }

  Future<void> _initPackageInfo() async {
    packageInfo = await PackageInfo.fromPlatform();
    setState(() {});
  }
}
